package common.classes;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class RedirectResponseThread implements Runnable {
	ArrayList<String> list;
	Socket socket;
	String filePath;
	
	public RedirectResponseThread(Socket socket, ArrayList<String> list, String filePath) {
		this.socket = socket;
		this.list = list;
		this.filePath = new String(filePath);
	}
	
	public void run() {
		try {
			OutputStream os = socket.getOutputStream();
			os.write("HTTP\\1.1 300 Multiple Choices\r\n".getBytes());
			os.write("Content-type: text/html\r\n\r\n".getBytes());
			
			os.write("<center>Sorry, but our server hasn't this content. Try Some of them</center>".getBytes());
			
			for (int i = 0; i < this.list.size(); i++)
				os.write(("<a href=\"" + this.list.get(i) + this.filePath + "\">" + this.list.get(i) + "</a><br>").getBytes());
			
			os.flush();
		} catch (FileNotFoundException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		} finally {
			try {
				socket.close();
			} catch (IOException e) {
				System.err.println(e);
			}
		}	
	}
}